<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="obj in list" :key="obj.id" :label="obj.id">{{
        obj.name
      }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 保存权限弹框中选项的值
      list: [] // 保存获取角色权限的值
    }
  },
  //   created() {
  //     this.loadRoles()
  //   },
  methods: {
    // 获取角色信息
    async loadRoles() {
      const res = await getRoles({ page: 1, pagesize: 100 })
      console.log(res)
      this.list = res.data.rows
      const info = await getUserDetailById(this.employeeId)
      //   console.log(info)
      this.$nextTick(() => {
        this.roleIds = info.data.roleIds
      })
    },
    // 保存当前角色信息
    async hSubmit() {
      try {
        const res = await assignRoles({ id: this.employeeId, roleIds: this.roleIds })
        console.log('保存角色', res)
        this.$emit('update-close')
        this.$message.success('添加角色成功')
      } catch (err) {
        // console.log()
        this.$message.error(err)
      }
    },
    abc() {
      this.roleIds = []
    },

    // 给父组件抛出事件关闭弹窗
    closeDialog() {
      this.$emit('close')
    }

  }
}
</script>
